package com.koicarp.university.graduate.service.service.information.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koicarp.university.graduate.service.dao.InformationDao;
import com.koicarp.university.graduate.service.dto.information.InformationWithTypeDto;
import com.koicarp.university.graduate.service.entity.information.Information;
import com.koicarp.university.graduate.service.service.information.IInformationService;
import com.koicarp.university.graduate.service.vo.information.InformationVo;
import com.koicarp.university.graudate.common.constant.BaseConstant;
import com.koicarp.university.graudate.common.utils.UniversityResponse;
import com.koicarp.university.graudate.common.utils.UniversityUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 【资讯管理：资讯信息表】(Information)表服务实现类
 *
 * @author liutao
 * @since 2020-12-04 16:06:07
 */
@Slf4j
@Service("informationService")
@RequiredArgsConstructor
public class InformationServiceImpl extends ServiceImpl<InformationDao,Information> implements IInformationService {

    private final InformationDao informationDao;


    @Override
    public UniversityResponse getInformationWithPage(InformationVo vo) {
        UniversityResponse response = new UniversityResponse();
        Page<Information> page = null;
        boolean findOne = UniversityUtil.isNotNull(vo.getId());
        boolean isPage = UniversityUtil.isNotNull(vo.getPageNum())
                && UniversityUtil.isNotNull(vo.getPageSize());
        Information information=new Information();
        BeanUtils.copyProperties(vo,information);
        if (!findOne && !isPage){
            response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
            Map listRes = new HashMap();
            listRes.put(BaseConstant.RESPONSE_RESULTSET,this.informationDao.queryAll(information));
            response.data(listRes);
            //response.data(this.tParkInfoDao.selectTParkInfoDto(tParkInfoDto));
            return response;
        }
        if (findOne){
            page = new Page<>(1,1,false);
        } else{
            page = new Page<>(vo.getPageNum(),vo.getPageSize(),true);
        }
        IPage<InformationWithTypeDto> result = null;
        result = this.informationDao.queryInformationWithPage(page, vo);
        if (findOne){
            List<InformationWithTypeDto> list = result.getRecords();
            if (list == null || list.size() == 0){
                response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
                response.message(BaseConstant.RESPONSE_MESSAGE_OPR_RESULT_NULL);
            } else {
                response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
                response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
                response.data(list.get(0));
            }
        } else {
            response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
            Map<String, Object> dataTable = UniversityUtil.getDataTable(result);
            response.data(dataTable);
        }
        return response;
    }

    @Override
    public UniversityResponse saveInformation(Information information) {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        information.setCreateTime(new Date());
        int i = this.informationDao.insert(information);
        if (i > 0){
            return response;
        }
        response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        return response;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Information queryById(Integer id) {
        return this.informationDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Information> queryAllByLimit(int offset, int limit) {
        return this.informationDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param information 实例对象
     * @return 实例对象
     */
    @Override
    public Information insert(Information information) {
        this.informationDao.insert(information);
        return information;
    }

    /**
     * 修改数据
     *
     * @param information 实例对象
     * @return 实例对象
     */
    @Override
    public UniversityResponse update(Information information) {
        int i = this.informationDao.update(information);
        UniversityResponse response=new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        if (i > 0){
            return response;
        }
        response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        return response;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public UniversityResponse deleteById(Integer id) {
        int i = this.informationDao.deleteById(id);
        UniversityResponse response=new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        if (i > 0){
            return response;
        }
        response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        return response;
    }

    @Override
    public UniversityResponse deleteList(List<Integer> list) {
        int i =this.informationDao.deleteListById(list);
        UniversityResponse response =new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        if (i<0){
            response.code(BaseConstant.RESPONSE_CODE_FAILED_500);
            response.message(BaseConstant.RESPONSE_MESSAGE_OPR_FAILED);
        }
        return response;
    }
}